package Diagnosis

import (
	"MedicalTreatment/global"
	model "MedicalTreatment/model"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/tealeg/xlsx"
	"log"
	"time"
)

type Res struct {
	Id                 uint
	Doctor_name        string `gorm:"type:int(100)"`
	PatientName        string `gorm:"type:int(100)"`
	Department         string `gorm:"type:varchar(100)"`
	SymptomDescription string `gorm:"type:varchar(100)"`
	Diagnosis          string `gorm:"type:varchar(100)"`
	IsHospital         string `gorm:"type:boolean"`
	HospitalStay       string `gorm:"type:varchar(100)"`
	Created_at         time.Time
	//DrugName           []string `gorm:"type:bigint(20)"`
	//Number             []int    `gorm:"type:int(100)"`
}
type Id struct {
	Id []int
}
type Dep struct {
	Name string
}
type Dru struct {
	Drug_id   int
	Name      string
	Number    string
	Result_id uint
}
type Drup struct {
	DrugId   int
	Name     string
	Number   int
	ResultId int
}

func DiaExp(r *gin.Engine) {
	r.POST("/diagnosisExport", func(context *gin.Context) {
		var idNumber Id
		if err := context.ShouldBindJSON(&idNumber); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
			return
		}
		//获取诊断结果的医患信息
		var frist Res
		var res []Res
		for i := 0; i < len(idNumber.Id); i++ {
			if err := global.DB.Model(model.Result{}).Where("id = ?", idNumber.Id[i]).Find(&frist).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
				return
			}
			res = append(res, frist)
		}

		//获取诊断结果的科室信息
		var p Dep
		var dep []Dep
		for i := 0; i < len(res); i++ {
			if err := global.DB.Model(model.Department{}).Select("name").Where("id = ?", res[i].Department).Find(&p).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
				return
			}
			dep = append(dep, p)
		}

		//获取诊断结果的药品ID以及个数

		var durp []Dru
		for i := 0; i < len(idNumber.Id); i++ {
			var dur []Dru
			if err := global.DB.Model(model.DrugList{}).Debug().Select("drug_id", "number", "result_id").Debug().Where("result_id = ?", idNumber.Id[i]).Find(&dur).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
				return
			}
			durp = append(durp, dur...)
			fmt.Println("循环中", durp)

		}
		fmt.Println("循环结束后", durp)
		for i := 0; i < len(durp); i++ {
			var druplist Drup
			if err := global.DB.Model(model.Drug{}).Select("name").Where("id = ?", durp[i].Drug_id).First(&druplist).Error; err != nil {
				context.JSON(200, gin.H{
					"Msg": err,
				})
			} else {
				durp[i].Name = druplist.Name
			}

		}
		fmt.Println("结果", durp)
		f := xlsx.NewFile()
		sheet, err := f.AddSheet("sheet1")
		if err != nil {
			log.Fatal(err)
		}
		//添加表头
		headerRow := sheet.AddRow()
		headerRow.SetHeight(30) //设置行高
		//添加表头列
		headerCell := headerRow.AddCell()
		headerCell.Value = "医生姓名"
		headerCell = headerRow.AddCell()
		headerCell.Value = "患者姓名"
		headerCell = headerRow.AddCell()
		headerCell.Value = "科室"
		headerCell = headerRow.AddCell()
		headerCell.Value = "症状描述"
		headerCell = headerRow.AddCell()
		headerCell.Value = "诊断结果"
		headerCell = headerRow.AddCell()
		headerCell.Value = "是否需要住院"
		headerCell = headerRow.AddCell()
		headerCell.Value = "住院时长（天）"
		headerCell = headerRow.AddCell()
		headerCell.Value = "药品清单"
		headerCell = headerRow.AddCell()
		headerCell.Value = "就诊日期"
		for i := 0; i < len(res); i++ {
			//创建数据行
			dataRow := sheet.AddRow()
			//创建数据列
			dataCell := dataRow.AddCell()
			dataCell.Value = res[i].Doctor_name
			dataCell = dataRow.AddCell()
			dataCell.Value = res[i].PatientName
			dataCell = dataRow.AddCell()
			dataCell.Value = dep[i].Name
			dataCell = dataRow.AddCell()
			dataCell.Value = res[i].SymptomDescription
			dataCell = dataRow.AddCell()
			dataCell.Value = res[i].Diagnosis
			dataCell = dataRow.AddCell()
			dataCell.Value = res[i].IsHospital
			dataCell = dataRow.AddCell()
			dataCell.Value = res[i].HospitalStay
			dataCell = dataRow.AddCell()
			var drops string
			for j := 0; j < len(durp); j++ {
				if res[i].Id == durp[j].Result_id {
					drops = drops + durp[j].Name + "*" + durp[j].Number + "盒" + ","
					dataCell.Value = drops
				}
			}
			dataCell = dataRow.AddCell()
			layout := "2006-01-02 15:04:05"
			data := res[i].Created_at.Format(layout)
			fmt.Println(data)
			datastr := data[:10]
			dataCell.Value = datastr
		}
		err = f.Save("诊疗结果数据.xlsx")
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("导出成功")
	})

}
